//
//  SecondViewController.m
//  UI 豆瓣完整版
//
//  Created by dllo on 15/12/31.
//  Copyright © 2015年 lanou. All rights reserved.
//

#import "SecondViewController.h"
#import "Model.h"
#import "MyCollectionViewCell.h"
#import "UIImageView+WebCache.h"
@interface SecondViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic, retain) NSMutableArray *arrModel;


@end

@implementation SecondViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}

- (void)dealloc {
    [_arrModel release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationController.navigationBar.translucent = NO;
    self.navigationItem.title = @"电影";
    // Do any additional setup after loading the view.
    
    [self createCollectionView];
    
    // 数据解析
    [self dataHandler];
    
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _arrModel.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    MyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    Model *model = _arrModel[indexPath.row];
    [cell.imgView sd_setImageWithURL:[NSURL URLWithString:model.small]];
    cell.model = model;
    
    return cell;
}

- (void)createCollectionView {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    // 滚动方向
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    // cell大小
    layout.itemSize = CGSizeMake((375 - 40) / 3, 150);
    // 最小行间距和最小列间距
    layout.minimumInteritemSpacing = 10;
    layout.minimumLineSpacing = 10;
    // 四周间距
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    
    
    // 创建
    UICollectionView *collection = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
    collection.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:collection];
    [collection release];
    
    // 设置代理人
    collection.delegate = self;
    collection.dataSource = self;
    // 注册
    [collection registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    
}

// 数据解析
- (void)dataHandler {
    NSString *path = [[NSBundle mainBundle] pathForResource:@"movie" ofType:@"plist"];
    NSArray *arr = [NSArray arrayWithContentsOfFile:path];
    
    

    self.arrModel = [NSMutableArray array];
    for (NSDictionary *dic in arr) {
        Model *model = [Model model];
        [model setValuesForKeysWithDictionary:dic];
        [_arrModel addObject:model];

    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
